/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t__resp_data_analysis.c                            */
/*  NOTE      =                                                    */
/*  DATE      = 2016/08/15 by jikw                                 */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include <dubbo/dubbo_core.h>
#include "frsutl.h"
#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "frreport_comm.h"
#include "frbyte_buffer.h"
#include "../../../../src/module/dubbo/config.h"

static void __list_report_node_free(Frlist_node *p_st_node)
{
    Frreport_data *p_st_report_data=NULL;
    if(p_st_node==NULL || p_st_node->data == NULL )
        return;

    p_st_report_data = (Frreport_data*)FRLIST_GET_NODE_DATA(p_st_node);
    if(p_st_report_data)
    {
        frreport_data_free(&p_st_report_data);
    }

    return;
}

static int __suite_init(void)
{

	return 0;
}

static int __suite_cleanup(void)
{

	return 0;
}

extern L7session_dubbo_private* __dubbo_private_create(void);
extern int __resp_data_analysis(Conntrack *p_st_conn
                                , Apm_packet *p_st_data_in
                                , Frhash_header *p_st_hash_report
                                , L7_session *p_st_session);
extern int dubbo_session_destroy(void *p_private_session);

static void case1(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_data_in = NULL;
    Frhash_header *p_st_hash_report = NULL;
    L7_session *p_st_session = NULL;

    i_ret = __resp_data_analysis(p_st_conn, p_st_data_in, p_st_hash_report, p_st_session);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);
    CU_ASSERT_PTR_EQUAL(p_st_session, NULL);

    return;
}

static void case2(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_data_in = NULL;
    Frhash_header *p_st_hash_report = NULL;
    L7_session *p_st_session = NULL;

    L7session_dubbo_private *p_st_private = NULL;
    dubbo_report_list *p_st_report = NULL;
	Frlist_node *p_st_node = NULL;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;

    p_st_data_in = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_data_in == NULL)
        goto ERROR;
    memset(p_st_data_in, 0x00, sizeof(Apm_packet));
    p_st_data_in->p_payload = (char *)"\xda\xbb\x02\x14\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x39\x91\x30\x36\x48\x65\x6c\x6c\x6f\x20\x77\x6f\x72\x6c\x64\x30\x2c\x20\x72\x65\x73\x70\x6f\x6e\x73\x65\x20\x66\x6f\x72\x6d\x20\x70\x72\x6f\x76\x69\x64\x65\x72\x3a\x20\x31\x30\x2e\x31\x2e\x31\x30\x2e\x37\x38\x3a\x32\x30\x38\x38\x30";
    p_st_data_in->ui_payload_len = 73;
    p_st_data_in->st_timestamp.tv_sec = 10;
    p_st_data_in->st_timestamp.tv_usec = 25;

    p_st_hash_report = frhash2_create2(2);
    CU_ASSERT_PTR_NOT_NULL(p_st_hash_report);

    p_st_session = (L7_session *)frmalloc(sizeof(L7_session));
    if(p_st_session == NULL)
        goto ERROR;
    memset(p_st_session, 0x00, sizeof(L7_session));
    p_st_session->p_session_private = __dubbo_private_create();
    if(p_st_session->p_session_private == NULL)
        goto ERROR;

    p_st_private = (L7session_dubbo_private*)p_st_session->p_session_private;

    p_st_private->st_l4_trans.st_request_time.tv_sec = 6;
    p_st_private->st_l4_trans.st_request_time.tv_usec = 25;

    p_st_private->p_report_list_dubbo  = (Frhash_header *)frmalloc(sizeof(Frhash_header));
    if (p_st_private->p_report_list_dubbo == NULL)
        goto ERROR;
    memset(p_st_private->p_report_list_dubbo, 0x00, sizeof(Frhash_header));
    frhash2_create(p_st_private->p_report_list_dubbo, 2);
    p_st_report = (dubbo_report_list *)frmalloc(sizeof(dubbo_report_list));
    memset(p_st_report, 0x00, sizeof(dubbo_report_list));
    p_st_node = frlist_node_create(p_st_report);
    frhash2_addLong(p_st_private->p_report_list_dubbo, 0, p_st_node);

    i_ret = __resp_data_analysis(p_st_conn, p_st_data_in, p_st_hash_report, p_st_session);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SET_RESP(APM_L7_ANALYZE_SUCC));
//    CU_ASSERT_EQUAL(p_st_private->st_l4_trans.l_response_bytes, 73);
//    CU_ASSERT_EQUAL(p_st_private->st_l4_trans.ul_response_time_us, 4000025);
//    CU_ASSERT_EQUAL(p_st_private->st_l4_trans.st_response_time_first_byte.tv_sec, 10);
//    CU_ASSERT_EQUAL(p_st_private->st_l4_trans.st_response_time_first_byte.tv_usec, 25);
//    CU_ASSERT_EQUAL(p_st_private->st_l4_trans.st_response_time.tv_sec, 10);
//    CU_ASSERT_EQUAL(p_st_private->st_l4_trans.st_response_time.tv_usec, 25);

    frfree(p_st_conn);
    frfree(p_st_data_in);
    frhash2_free2(p_st_hash_report, __list_report_node_free);
    dubbo_session_destroy(p_st_session->p_session_private);
    frfree(p_st_session);

    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_data_in)
        frfree(p_st_data_in);
    if(p_st_hash_report)
        frhash2_free2(p_st_hash_report, __list_report_node_free);
    if(p_st_session)
    {
        dubbo_session_destroy(p_st_session->p_session_private);
        frfree(p_st_session);
    }

    return;
}

static void case3(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_data_in = NULL;
    Frhash_header *p_st_hash_report = NULL;
    L7_session *p_st_session = NULL;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;

    p_st_data_in = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_data_in == NULL)
        goto ERROR;
    memset(p_st_data_in, 0x00, sizeof(Apm_packet));
    p_st_data_in->p_payload = (char *)"\xda\xbb\x02\x14\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x39\x91\x30\x36\x48\x65\x6c\x6c\x6f\x20\x77\x6f\x72\x6c\x64\x30\x2c\x20\x72\x65\x73\x70\x6f\x6e\x73\x65\x20\x66\x6f\x72\x6d\x20\x70\x72\x6f\x76\x69\x64\x65\x72\x3a\x20\x31\x30\x2e\x31\x2e\x31\x30\x2e\x37\x38\x3a\x32\x30";
    p_st_data_in->ui_payload_len = 70;
    p_st_data_in->st_timestamp.tv_sec = 10;
    p_st_data_in->st_timestamp.tv_usec = 25;

    p_st_hash_report = frhash2_create2(2);
    CU_ASSERT_PTR_NOT_NULL(p_st_hash_report);

    p_st_session = (L7_session *)frmalloc(sizeof(L7_session));
    if(p_st_session == NULL)
        goto ERROR;
    memset(p_st_session, 0x00, sizeof(L7_session));
    p_st_session->p_session_private = __dubbo_private_create();
    if(p_st_session->p_session_private == NULL)
        goto ERROR;

    ((L7session_dubbo_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time.tv_sec = 6;
    ((L7session_dubbo_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time.tv_usec = 25;

    i_ret = __resp_data_analysis(p_st_conn, p_st_data_in, p_st_hash_report, p_st_session);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_TODO);
//    CU_ASSERT_EQUAL(((L7session_dubbo_private *)(p_st_session->p_session_private))->st_l4_trans.l_response_bytes, 70);
//    CU_ASSERT_EQUAL(((L7session_dubbo_private *)(p_st_session->p_session_private))->st_l4_trans.ul_response_time_us, 4000025);
//    CU_ASSERT_EQUAL(((L7session_dubbo_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_sec, 10);
//    CU_ASSERT_EQUAL(((L7session_dubbo_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_usec, 25);
//    CU_ASSERT_EQUAL(((L7session_dubbo_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time.tv_sec, 10);
//    CU_ASSERT_EQUAL(((L7session_dubbo_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time.tv_usec, 25);

    frfree(p_st_conn);
    frfree(p_st_data_in);
    frhash2_free2(p_st_hash_report, __list_report_node_free);
    dubbo_session_destroy(p_st_session->p_session_private);
    frfree(p_st_session);

    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_data_in)
        frfree(p_st_data_in);
    if(p_st_hash_report)
        frhash2_free2(p_st_hash_report, __list_report_node_free);
    if(p_st_session)
    {
        dubbo_session_destroy(p_st_session->p_session_private);
        frfree(p_st_session);
    }

    return;
}

static void case4(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_data_in = NULL;
    Frhash_header *p_st_hash_report = NULL;
    L7_session *p_st_session = NULL;
    L7session_dubbo_private *p_st_private = NULL; 
    char *p_fr = (char *)"\xda\xbb\x02\x14\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x39\x91\x30\x36\x48\x65\x6c\x6c\x6f\x20\x77\x6f\x72\x6c\x64\x30\x2c\x20\x72\x65\x73\x70\x6f\x6e\x73\x65\x20\x66\x6f\x72\x6d\x20\x70\x72\x6f\x76\x69\x64\x65\x72\x3a\x20\x31\x30\x2e\x31\x2e\x31\x30\x2e\x37\x38\x3a\x32\x30";

    dubbo_report_list *p_st_report = NULL;
	Frlist_node *p_st_node = NULL;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;

    p_st_data_in = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_data_in == NULL)
        goto ERROR;
    memset(p_st_data_in, 0x00, sizeof(Apm_packet));
    p_st_data_in->p_payload = (char *)"\x38\x38\x30";
    p_st_data_in->ui_payload_len = 3;
    p_st_data_in->st_timestamp.tv_sec = 12;
    p_st_data_in->st_timestamp.tv_usec = 25;

    p_st_hash_report = frhash2_create2(2);
    CU_ASSERT_PTR_NOT_NULL(p_st_hash_report);

    p_st_session = (L7_session *)frmalloc(sizeof(L7_session));
    if(p_st_session == NULL)
        goto ERROR;
    memset(p_st_session, 0x00, sizeof(L7_session));
    p_st_session->p_session_private = __dubbo_private_create();
    if(p_st_session->p_session_private == NULL)
        goto ERROR;
    p_st_private = (L7session_dubbo_private*)p_st_session->p_session_private;

    p_st_private->p_st_body_data_resp = frreport_body_new_l7(p_st_conn);  
    p_st_private->p_resp_packet = bytebuff_creat();
    if(bytebuff_add(p_st_private->p_resp_packet, p_fr, 70) != FR_SUCC)
        goto ERROR;
    p_st_private->st_l4_trans.l_response_bytes = 70;
    p_st_private->st_l4_trans.ul_response_time_us = 0;
    p_st_private->st_l4_trans.st_response_time_first_byte.tv_sec = 10;
    p_st_private->st_l4_trans.st_response_time_first_byte.tv_usec = 25;
    p_st_private->st_l4_trans.st_request_time_first_byte.tv_sec = 0;
    p_st_private->st_l4_trans.st_request_time_first_byte.tv_usec = 0;

    p_st_private->p_report_list_dubbo  = (Frhash_header *)frmalloc(sizeof(Frhash_header));
    if (p_st_private->p_report_list_dubbo == NULL)
        goto ERROR;
    memset(p_st_private->p_report_list_dubbo, 0x00, sizeof(Frhash_header));
    frhash2_create(p_st_private->p_report_list_dubbo, 2);
    p_st_report = (dubbo_report_list *)frmalloc(sizeof(dubbo_report_list));
    memset(p_st_report, 0x00, sizeof(dubbo_report_list));
    p_st_node = frlist_node_create(p_st_report);
    frhash2_addLong(p_st_private->p_report_list_dubbo, 0, p_st_node);

    i_ret = __resp_data_analysis(p_st_conn, p_st_data_in, p_st_hash_report, p_st_session);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SET_RESP(APM_L7_ANALYZE_SUCC));
//    CU_ASSERT_EQUAL(((L7session_dubbo_private *)(p_st_session->p_session_private))->st_l4_trans.l_response_bytes, 73);
//    CU_ASSERT_EQUAL(((L7session_dubbo_private *)(p_st_session->p_session_private))->st_l4_trans.ul_response_time_us, 4000025);
//    CU_ASSERT_EQUAL(((L7session_dubbo_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_sec, 10);
//    CU_ASSERT_EQUAL(((L7session_dubbo_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_usec, 25);
//    CU_ASSERT_EQUAL(((L7session_dubbo_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time.tv_sec, 12);
//    CU_ASSERT_EQUAL(((L7session_dubbo_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time.tv_usec, 25);

    frfree(p_st_conn);
    frfree(p_st_data_in);
    frhash2_free2(p_st_hash_report, __list_report_node_free);
    dubbo_session_destroy(p_st_session->p_session_private);
    frfree(p_st_session);

    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_data_in)
        frfree(p_st_data_in);
    if(p_st_hash_report)
        frhash2_free2(p_st_hash_report, __list_report_node_free);
    if(p_st_session)
    {
        dubbo_session_destroy(p_st_session->p_session_private);
        frfree(p_st_session);
    }

    return;
}

int add_suite__resp_data_analysis(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" __resp_data_analysis()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", case1) == NULL ||
        CU_add_test(pSuite, "case2", case2) == NULL ||
        CU_add_test(pSuite, "case3", case3) == NULL ||
        CU_add_test(pSuite, "case4", case4) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
